package edu.tomee.servlets;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.tomee.db.DatabaseConnection;
import edu.tomee.db.MsgBox;
import edu.tomee.db.UserInfo;


@WebServlet({"/login","/logout"})
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String action = request.getServletPath();
		if( "/logout".equals(action) ){
			processLogout(request, response);
		}
		if("/login".equals(action)){
			processLogin(request, response);
		}
	}
	private void processLogout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}

	private void processLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Object userSession = session.getAttribute("rslt");
		if( null != userSession ){
			 request.getRequestDispatcher("myaccount.jsp").include(request, response);
		}else{
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String eid = request.getParameter("T1");
		String pwd = request.getParameter("T2");
		HttpSession session = request.getSession();
		session.setAttribute("eid", eid);
		request.setAttribute("pwd", pwd);
		DatabaseConnection db = new DatabaseConnection();
		UserInfo result = db.logincheck(eid, pwd);
		session.setAttribute("rslt", result);
	    request.setAttribute("eid", eid);
	    if( null != result ){
	    	int uid = result.getUid();
	    	session.setAttribute("userId", uid);
	    	Map<String, String> mapdtl = DatabaseConnection.usersMap();
	    	System.out.println("mapdtl "+mapdtl);
	    	session.setAttribute("idnam", mapdtl);
		    List<MsgBox> msgs = db.getMsgs(uid);
		    session.setAttribute("msgs", msgs);
		    if( null != msgs ){
		    	session.setAttribute("msgsCount", msgs.size());
		    }
		    List<UserInfo> al_user = db.allUsers(uid);
		    session.setAttribute("al_user", al_user);
		    request.getSession().setAttribute("user", eid);
		    request.getRequestDispatcher("myaccount.jsp").forward(request, response);
	    }else{
	    	request.setAttribute("errorMsg", "Invalid Credentials");
	    	request.getRequestDispatcher("login.jsp").forward(request, response);
	    }
	    
		
	}

}
